package com.cf.controller;

import com.cf.dao.PowerDao;
import com.cf.entity.Power;
import com.cf.entity.Result;
import com.cf.entity.Role;
import com.cf.service.RoleService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
public class RoleController {
    @Autowired
    private RoleService roleService;
    @Autowired
    private PowerDao powerDao;

    //分页查询角色集合的接口
    @RequestMapping("/selectRoles")
    public Object selectRoles(){
        List<Role> page = roleService.selectRoles();
        if (page.size()!=0){
            return new Result(200,"查询成功",page);
        }else {
            return new Result(400,"查询失败",null);
        }
    }

    //分页查询角色集合的接口
    @RequestMapping("/selectRolesByName")
    public Object selectRolesByName(int pagenum,int pagesize,String name){
        Map map = new HashMap();
        map.put("name",name);
        PageInfo<Role> page = roleService.selectRolesByName(pagenum,pagesize,map);
        if (page.getList().size()!=0){
            return new Result(200,"查询成功",page);
        }else {
            return new Result(400,"查询失败",null);
        }
    }

    //根据角色编号来改变其下的所有的权限
    @RequestMapping("/updateRolePower")
    public Object updateRolePower(long rid,String pids){
        List<Power> powers = powerDao.selectPower(rid);
        //先根据rid把中间表中的数据删除
        int i = 0;
        if (powers.size()!=0){//不等于0，说明中间表中有该对象的关联关系
            i = roleService.updateRoleAndPower(rid,pids);
            if (i!=0){
                return new Result(200,"修改成功",null);
            }else {
                return new Result(400,"修改失败",null);
            }
        }else {
            String[] split = pids.split(",");
            int i1=0;
            for (String s:split){
                i1 = roleService.addRoleAndPower(rid, Long.parseLong(s));
            }
            if (i1!=0){
                return new Result(200,"修改成功",null);
            }else {
                return new Result(400,"修改失败",null);
            }
        }
    }
    @RequestMapping("/deleteRoel")
    public Object deleteRole(long roleid){
        int i = roleService.deleteRole(roleid);
        if (i!=0){
            return new Result(200,"删除成功",null);
        }else {
            return new Result(400,"删除失败",null);
        }
    }

    @RequestMapping("/updateById")
    public Object updateById(Role role){
        int i = roleService.updateById(role);
        if (i!=0){
            return new Result(200,"修改成功",null);
        }else {
            return new Result(400,"修改失败",null);
        }
    }

    @RequestMapping("/selectByRoleid")
    public Object selectByRoleid(long id){
        Role role = roleService.selectByRoleid(id);
        if(role!=null){
            return new Result(200,"查询当前角色成功",role);
        }else {
            return new Result(400,"查询当前角色失败",null);
        }
    }
}
